﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace ObjectSearch.Configuration
{
    [global::System.AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, Inherited = true, AllowMultiple = false)]
    public sealed class FieldAttribute : Attribute
    {
        /// <summary>
        /// A private field used to hold the value for Name property.
        /// </summary>
        private string _Name;

        public string Name
        {
            get
            {
                return _Name;
            }
            set
            {
                _Name = value;
            }
        }

        /// <summary>
        /// A private field used to hold the value for IndexOptions property.
        /// </summary>
        private IndexOptions _IndexOptions;

        /// <summary>
        /// Gets or sets how a field should be indexed.
        /// </summary>
        public IndexOptions IndexOptions
        {
            get
            {
                return _IndexOptions;
            }
            set
            {
                _IndexOptions = value;
            }
        }

        /// <summary>
        /// A private field used to hold the value for StorageOptions property.
        /// </summary>
        private StorageOptions _StorageOptions;

        /// <summary>
        /// Gets or sets how the field should be stored.
        /// </summary>
        public StorageOptions StorageOptions
        {
            get
            {
                return _StorageOptions;
            }
            set
            {
                _StorageOptions = value;
            }
        }

        /// <summary>
        /// A private field used to hold the value for TermVectorOptions property.
        /// </summary>
        private TermVectorOptions _TermVectorOptions;

        /// <summary>
        /// Gets or sets how a field should have term vectors.
        /// </summary>
        public TermVectorOptions TermVectorOptions
        {
            get
            {
                return _TermVectorOptions;
            }
            set
            {
                _TermVectorOptions = value;
            }
        }

        /// <summary>
        /// A private field used to hold the value for Boost property.
        /// </summary>
        private float _Boost;

        /// <summary>
        /// <para>
        /// Gets or sets the boost factor for hits for this field.
        /// This value will bemultiplied into the score of all hits on this this field of this
        /// document.
        /// </para>
        /// <para>
        /// The default value is 1.0.
        /// </para>
        /// <para>
        /// Note: this value is not stored directly with the document in the index.
        /// Documents returned from IndexReader and
        /// Hits.Doc(int) may thus not have the same value present as when
        /// this field was indexed.
        /// </para>
        /// </summary>
        public float Boost
        {
            get
            {
                return _Boost;
            }
            set
            {
                _Boost = value;
            }
        }

        /// <summary>
        /// A private field used to hold the value for SortingOptions property.
        /// </summary>
        private SortOptions _SortingOptions;

        public SortOptions SortingOptions
        {
            get
            {
                return _SortingOptions;
            }
            set
            {
                _SortingOptions = value;
            }
        }




        /// <summary>
        /// A private field used to hold the value for IsDefaultSearchField property.
        /// </summary>
        private bool _IsDefaultSearchField;

        public bool IsDefaultSearchField
        {
            get
            {
                return _IsDefaultSearchField;
            }
            set
            {
                _IsDefaultSearchField = value;
            }
        }

        /// <summary>
        /// A private field used to hold the value for IsUniqueKey property.
        /// </summary>
        private bool _IsUniqueKey;

        public bool IsUniqueKey
        {
            get
            {
                return _IsUniqueKey;
            }
            set
            {
                _IsUniqueKey = value;
            }
        }







        /// <summary>
        /// Initializes a new instance of the <see cref="FieldAttribute"/> class.
        /// </summary>
        public FieldAttribute()
        {
            _Name = null;
            _Boost = 1.0f;
            _IndexOptions = IndexOptions.NotIndexed;
            _StorageOptions = StorageOptions.NotStored;
            _TermVectorOptions = TermVectorOptions.NotStored;
            _IsDefaultSearchField = false;
            _SortingOptions = SortOptions.None;
            _IsUniqueKey = false;
        }


        public static FieldAttribute GetFieldAttribute(MemberInfo memberInfo)
        {
            object[] obj = memberInfo.GetCustomAttributes(typeof(FieldAttribute), true);

            if (obj == null || obj.Length == 0)
                return null;
            else
                return obj[0] as FieldAttribute;
        }
    }
}
